package it.fantacalcio.struts.form;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import it.fantacalcio.model.Role;
import it.fantacalcio.spring.service.MyServiceFactory;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class UserForm extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;
	private String nome;
	private String cognome;
	private String username;
	private String password;
	private String attivo;
	private Long idRole;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome.equals("") ? null : nome;
	}
	public String getCognome() {
		return cognome;
	}
	public void setCognome(String cognome) {
		this.cognome = cognome.equals("") ? null : cognome;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username.equals("") ? null : username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password.equals("") ? null : password;
	}
	public String getAttivo() {
		return attivo;
	}
	public void setAttivo(String attivo) {
		this.attivo = attivo.equals("") ? null : attivo;
	}
	public Long getIdRole() {
		return idRole;
	}
	public void setIdRole(Long idRole) {
		this.idRole = idRole;
	}
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		
		ActionErrors errors = new ActionErrors();
		if(this.getUsername()==null || this.getUsername().equals("")){
		 	errors.add("username", new ActionMessage("errors.user.username.stringa_vuota"));
		}
		if(this.getPassword()==null || this.getPassword().equals("")){
		 	errors.add("password", new ActionMessage("errors.user.password.stringa_vuota"));
		}
		if(this.getNome()==null || this.getNome().equals("")){
		 	errors.add("nome", new ActionMessage("errors.user.nome.stringa_vuota"));
		}
		if(this.getCognome()==null || this.getCognome().equals("")){
		 	errors.add("cognome", new ActionMessage("errors.user.cognome.stringa_vuota"));
		}
		
		// Account
		String pagina = request.getParameter("pagina");
		if(pagina == null || !pagina.equals("account")){
			if(this.getAttivo()==null || this.getAttivo().equals("")){
			 	errors.add("attivo", new ActionMessage("errors.user.attivo.stringa_vuota"));
			}
			if (this.getIdRole()<1){
				errors.add("idRole", new ActionMessage("errors.user.role.non_selezionato"));
			}
		}
		
		List<Role> listaRole=new ArrayList<Role>();
		if(errors.size()>0){
			try{
				listaRole=MyServiceFactory.getRoleService().list();			
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		request.setAttribute("listaRole", listaRole);
		return errors;
	}

}
